package com.controller;

import com.entity.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.service.AnimeService;
import com.service.LoginService;
import com.sun.deploy.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WZ
 * @version 1.0
 * @date 2021/8/18 16:15
 */
@Controller
@CrossOrigin
public class AnimeController {
    @Autowired
  private AnimeService animeService;

    @Autowired
    private LoginService loginService;

    @RequestMapping("/animekiaraDef")
    public  String animekira(ModelMap modelMap){

//        modelMap.addAttribute("nameScope","");
//        modelMap.addAttribute("formScope","");
//        modelMap.addAttribute("sexScope","");
//        modelMap.addAttribute("scoreScope","");
//        modelMap.addAttribute("urlScope","");
//        AnimeKira animeKira=animeService.findKira("爱尔奎特");
//        modelMap.addAttribute("nameScope",animeKira.getName());
//        modelMap.addAttribute("formScope",animeKira.getFromWhere());
//        modelMap.addAttribute("sexScope",animeKira.getSex());
//        modelMap.addAttribute("scoreScope",animeKira.getScore());
//        modelMap.addAttribute("urlScope",animeKira.getUrl());
        return  "animekiara";
    }
    @RequestMapping(value = "/animekiaraFindByName",method = RequestMethod.GET)
    @ResponseBody
//    @CrossOrigin(origins = {"http://47.106.124.209:8080","null"})
//    @CrossOrigin
    public SelectResBody animekiraFindByName(@RequestParam(value = "kiaRaName",required = false,defaultValue = "") String name,@RequestHeader(required = false,defaultValue ="" ) String token
    ,@RequestParam(value = "pageSize",required = false,defaultValue = "0")int pageSize,@RequestParam(value = "pageNum",required = false,defaultValue = "0")int pageNum
    ){

        if(LoginController.existToken(token)){
            if (hasRole("11",token)||hasRole("2",token)||hasRole("1",token)){
                Page page=PageHelper.startPage(pageNum,pageSize);// 用这个插件的话会直接把下面这个kira集合的返回数据给改了，比如第4*10 第4页返回的数据就是空的 !kira.isEmpty() 返回false
                List<AnimeKira> kira = animeService.findKira(name);
                PageInfo pageInfo=new PageInfo(kira);
                if(!kira.isEmpty()){
                    //num2  size10
//                if (pageSize!=0&&pageNum!=0){
//                    List<AnimeKira> klist=new ArrayList<>();
//                    for (int i=0;i<kira.size();i++){
//                        if (i>=(pageNum-1)*pageSize&&i<pageSize*pageNum){
//                            klist.add(kira.get(i));
//                        }
//                    }
                    SelectResBody res=new SelectResBody("200","成功",pageSize,pageNum,kira,(int)pageInfo.getTotal());
                    return  res;
//                }else {
//                    SelectResBody res=new SelectResBody("200","成功",kira);
//                    return  res;
//                }


                }else {
                    SelectResBody res=new SelectResBody("500","NO Resutls",pageInfo.getPageSize(),pageInfo.getPageNum(),kira,(int)pageInfo.getTotal());
                    return  res;
                }
            }else{
                SelectResBody res=new SelectResBody("403","兄弟，你没得权限");
                return  res;

            }

        }else {
            SelectResBody res=new SelectResBody("401","token无效或过期");
            return  res;

        }



    }
//    @RequestMapping(value = "/addKiaRa",method = RequestMethod.POST)
//    @ResponseBody
//    @CrossOrigin
//    public UpdateKiaRaResBody addKiaRa(@RequestBody AnimeKira kira,@RequestHeader(required = false,defaultValue ="" ) String token){
//          //直接传 AnimeKira对象给服务层方法 处理数据
//        long currenttime=System.currentTimeMillis();
//        String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);
//if(LoginController.existToken(token)){
//
//    String name = kira.getName();
//
//    if(name==null) {
//
//        UpdateKiaRaResBody res=new UpdateKiaRaResBody("404","亲，name不要为空噢",date1);
//        return res;
//    }
//    else {
//        animeService.addKiaRa(kira);
//        UpdateKiaRaResBody res=new UpdateKiaRaResBody("200","成功",date1);
//        return res;
//    }
//}else {
//    UpdateKiaRaResBody res=new UpdateKiaRaResBody("403","token无效或过期",date1);
//    return res;
//}
//
//            //在这里调用 服务层方法处理数据库 传的是个AnimeKira实体类
//
//
//    }
    @RequestMapping(value = "/addsss",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public AnimeKira addkkk(@RequestBody AnimeKira kira){


        return kira;
    }

    @RequestMapping(value = "/deleteKiaRa",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
//    @RequestHeader(value = "token",required = false,defaultValue = ""
    public UpdateKiaRaResBody deleteKiaRa(@RequestBody AnimeKira kira,@RequestHeader(required = false,defaultValue ="" ) String token){


        long currenttime=System.currentTimeMillis();
        String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);

if(LoginController.existToken(token)){
    if(hasRole("14",token)||hasRole("2",token)||hasRole("1",token)){
        if(kira.getId()==null){
            UpdateKiaRaResBody res=new UpdateKiaRaResBody("404","id不能为空",date1);
            return  res;
        }else {

            List<AnimeKira> kiraByid = animeService.findKiraByid(kira.getId());

            if(kiraByid.isEmpty()){
                UpdateKiaRaResBody res=new UpdateKiaRaResBody("500","没这角色你删空气呢",date1);
                return  res;
            }else {
                Number nid=kira.getId();
                Integer iddd=nid.intValue();
                kira.setId(iddd);
                animeService.deleteKiaRa(kira);
                UpdateKiaRaResBody res=new UpdateKiaRaResBody("200","成功",date1);
                return  res;
            }

        }
    }else {
        UpdateKiaRaResBody res=new UpdateKiaRaResBody("403","兄弟，你没得权限",date1);
        return  res;
    }

}else {
    UpdateKiaRaResBody res=new UpdateKiaRaResBody("401","token无效或过期",date1);
    return  res;
}
    }



    //修改
    @RequestMapping(value = "/updateKiaRa",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public  UpdateKiaRaResBody updateKiaRa(@RequestBody AnimeKira kira,@RequestHeader(required = false,defaultValue ="")String token){
        long currenttime=System.currentTimeMillis();
        String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);
        if(LoginController.existToken(token)){
            String name = kira.getName();
            long iddl=System.currentTimeMillis()/100000;
            int idd=(int)iddl;
            if(kira.getId()==null){
                if(hasRole("13",token)||hasRole("2",token)||hasRole("1",token)){
                    if(name==null) {

                        UpdateKiaRaResBody res=new UpdateKiaRaResBody("404","亲，name不要为空噢",date1);
                        return res;
                    }
                    else {
                        kira.setId(idd);
                        animeService.addKiaRa(kira);
                        UpdateKiaRaResBody res=new UpdateKiaRaResBody("200","成功",date1);
                        return res;
                    }
                }else {
                    UpdateKiaRaResBody res=new UpdateKiaRaResBody("403","没有权限",date1);
                    return  res;
                }

            }else {
             if(hasRole("12",token)||hasRole("2",token)||hasRole("1",token)){
                 List<AnimeKira> kiraByid = animeService.findKiraByid(kira.getId());

                 if(kiraByid.isEmpty()){
                     UpdateKiaRaResBody res=new UpdateKiaRaResBody("500","不存在该角色id，请修改",date1);
                     return  res;
                 }else {
                     animeService.updateKiaRa(kira);

                     UpdateKiaRaResBody res=new UpdateKiaRaResBody("200","成功",date1);
                     return  res;
                 }
             }else{
                 UpdateKiaRaResBody res=new UpdateKiaRaResBody("403","没有权限",date1);
                 return  res;
             }

            }

        }else {
            UpdateKiaRaResBody res=new UpdateKiaRaResBody("401","token无效或过期",date1);
            return  res;
        }


    }
@RequestMapping(value = "/findAllAnimes",method = RequestMethod.POST)
@ResponseBody
@CrossOrigin
    public  SelectResBody findAllAnime(@RequestBody AnimeListRequestBody alrb ,@RequestHeader(required = false,defaultValue = "")String token){



    if(hasRole("21",token)||hasRole("3",token)||hasRole("1",token)){
        Page page=PageHelper.startPage(alrb.getPageNum(),alrb.getPageSize());
        List<Anime> allAnime = animeService.findAllAnime(alrb.getTitle());
        PageInfo pageInfo=new PageInfo(allAnime);
        if(!allAnime.isEmpty()){
            SelectResBody resBody=new SelectResBody("200","成功",pageInfo.getPageSize(),pageInfo.getPageNum(),allAnime,(int)pageInfo.getTotal());
            return resBody;
        }else {
            SelectResBody resBody=new SelectResBody("500","NO Results",pageInfo.getPageSize(),pageInfo.getPageNum(),allAnime,(int)pageInfo.getTotal());
            return resBody;
        }
    }else {
        SelectResBody res=new SelectResBody("403","兄弟，你没得权限");
        return  res;
    }

    }

    @RequestMapping(value = "/updateAnimeList",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
public UpdateKiaRaResBody updateAnimeList(@RequestBody Anime anime,@RequestHeader(defaultValue = "",required = false)String token){
        long currenttime=System.currentTimeMillis();
        String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);




            if(StringUtil.isEmpty(anime.getId())||anime.getId().isEmpty()){
                if(hasRole("23",token)||hasRole("3",token)||hasRole("1",token)){
                    if(anime.getTitle().isEmpty()){
                        return new UpdateKiaRaResBody("500","标题能别不写吗 亲",date1);
                    }else {
                        long idl=System.currentTimeMillis()/1000;
                        String ids= String.valueOf(idl);
                        anime.setId(ids);
                        animeService.addAnime(anime);

                        return new UpdateKiaRaResBody("200","成功",date1);
                    }

                }else {
                    return new UpdateKiaRaResBody("403","没有权限",date1);
                }
            }else {
                if(hasRole("22",token)||hasRole("3",token)||hasRole("1",token)){
                    if(anime.getTitle().isEmpty()){
                        return new UpdateKiaRaResBody("500","标题能别不写吗 亲",date1);
                    }else {
                        animeService.updateAnime(anime);
                        return new UpdateKiaRaResBody("200","成功",date1);
                    }

                }else {
                    return new UpdateKiaRaResBody("403","没有权限",date1);
                }

            }

    }

    @RequestMapping(value = "/deleteAnime",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public UpdateKiaRaResBody deleteAnime(@RequestBody Anime anime,@RequestHeader(required = false,defaultValue = "")String token){
        long currenttime=System.currentTimeMillis();
        String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);
        if(hasRole("24",token)||hasRole("3",token)||hasRole("1",token)){
            animeService.deleteAnime(anime);
            return new UpdateKiaRaResBody("200","成功",date1);
        }else {
            return new UpdateKiaRaResBody("403","没有权限",date1);
        }

    }
    @PostMapping(value = "/deleteKiaRas")
    @ResponseBody
    @CrossOrigin
    public UpdateKiaRaResBody deleteKiaRas(@RequestBody BatchDelete ids,@RequestHeader(required = false,defaultValue = "")String token){
        long currenttime=System.currentTimeMillis();
        String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);
        if(hasRole("14",token)||hasRole("2",token)||hasRole("1",token)){
            animeService.deleteKiaRas(ids);
            return new UpdateKiaRaResBody("200","成功",date1);
        }else {
            return new UpdateKiaRaResBody("403","没有权限",date1);
        }

    }

    @GetMapping (value = "/animeTypeTable")
    @ResponseBody
    @CrossOrigin
    public SelectResBody animeTypeTable(){
        List<AnimeType> animeTypes = animeService.animeTypeTable();
        return new SelectResBody("200","成功",animeTypes);
    }

    public  Boolean hasRole(String roleId,String token){
        String userId=null;
        Boolean RoleBoolean=false;
             for(int i=0;i<LoginController.tokenLists.size();i++){
                 if (token.equals(LoginController.tokenLists.get(i).getToken())){
                     userId=LoginController.tokenLists.get(i).getUserId();
                     break;
                 }
             }
        List<UserRole> findroles = loginService.findroles(userId);

             for(int j=0;j<findroles.size();j++){
                 if (roleId.equals(findroles.get(j).getRoleId())){
                     RoleBoolean=true;
                 }
             }
return RoleBoolean;
    }
    @GetMapping("/rolesTable")
    @ResponseBody
    @CrossOrigin
    public SelectResBody rolesTable(){
        List<UserRole> userRoles=loginService.allRole();
        return new SelectResBody("200","成功",userRoles);
    }

    //获得角色/动画数量

    @GetMapping("/getCount")
    @ResponseBody
    @CrossOrigin
    public  SelectResBody getCountOfKiara(){
        CountNum countNum=animeService.getCount();
        List<CountNum> countNums=new ArrayList<>();
        countNums.add(countNum);
        return  new SelectResBody("200","成功",countNums);
    }
}
